## Master Script 
## Political Hostility in America (Druckman et al.)

## This is the master file that calls all of the sub-files 
## Each chapter has a sub-file to produce the tables/figures in that chapter 
## Tables/Figures can be found in the relevant folder 

## Load all libraries 
library(tidyverse)
library(haven)  
library(readxl)
library(janitor)
library(modelsummary)
library(psych) 
library(ggpubr) 
library(ggeffects)
#library(panelr) 
library(sensemakr) 
library(lfe) 
library(stargazer) 
library(polycor) 
library(forcats)
library(car) 
library(weights) 
library(stringr)

## Set the working directory 
## Tables/Figures then get set via relative directories in files 
## MUST CHANGE THIS TO BE RELATIVE TO YOUR MACHINE 
setwd("YOUR FILE PATH GOES HERE IT WILL NOT WORK WITHOUT IT")

## Read in the data used in the book and do some preliminary cleaning 
data <- read_dta(file="for_r_for_graphs_y.dta") 

## remove three sets of respondents: 
## Pure Independents 
## Those who switch parties 
## Those in condition 13 (not in original experiment)

## Pure Independents 
data <- data %>% 
  mutate(pure_ind = ifelse(partyid_w1 == 4 & 
                      partyid_w3 == 4 & partyid_w4 == 4,1,0)) 
## roughly 5% of the sample 

## Party Switchers 
## can change strength, but can't go D --> R or R --> D 
data <- data %>% 
  mutate(party_switcher = case_when(
    partyid_w1 < 4 & partyid_w4 < 4 ~ 0,
    partyid_w1 > 4 & partyid_w4 > 4 ~ 0,
    partyid_w1 > 4 & partyid_w4 < 4 ~ 1,
    partyid_w1 < 4 & partyid_w4 > 4 ~ 1)) 
## roughly 6% of the sample 

## People in Condition 13 
data <- data %>% 
  mutate(c13 = ifelse(condition2 == 13,1,0)) 

## Remove these people from the sample 
data <- data %>% 
  filter(pure_ind == 0 & party_switcher == 0 | c13 == 0) 

## add quantiles for graphs 
ap_quantiles <- quantile(data$apstable, probs=c(0.05,0.10,0.33,0.5,0.67,0.90,0.95), na.rm=T) 
## small quantiles for busier graphs 
ap_quantiles_small <- quantile(data$apstable, probs=c(0.05,0.33,0.67,0.95), na.rm=T) 

## Remove the top and bottom 5% from the sample 
## removes weird-non-linearities from odd folks 
data <- data %>% 
  filter(apstable > ap_quantiles[1] & 
           apstable < ap_quantiles[7]) 

## For faceting graphs: year as a factor variable 
data$factor_year <- factor(data$year,
                           labels = c("July 2019",
                                      "April 2020",
                                      "October 2020",
                                      "April 2021")) 

## Run the individual chapter files 
## Note that Chapter 3 gets run separately because it uses some full data to introduce why we exclude those above 
source("Code/chapter_1.r") 
## no analysis done in chapter 2, so no file  
## because chapter 3 includes some data w/o exclusion restrictions above, need to run it separately 
#source("Code/chapter_3.r") 
source("Code/chapter_4.r")
source("Code/chapter_5.r") 
source("Code/chapter_6.r") 
source("Code/chapter_7_compromise.r")
source("Code/chapter_8_norms.r")
